package com.starinside.wp.repository;

import com.starinside.wp.domain.liveRoom.LiveRoom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Repository
public interface LiveRoomRepository extends JpaRepository<LiveRoom, String> {

    @Query(value = "select l.* from liveroom l " +
//            "left join area a on l.aid = a.id " +
            "where (teachingAreaId in (" +
            "select teachingAreaId from rel_classes_teaching_area " +
            "where classesId in (" +
            "select classesId from rel_classes_children where childrenId in ?1)) or teachingAreaId in (" +
            "select id from teaching_area where areaId in (select aid from children where id in ?1) and isPublic = 1" +
            ")) and l.status = 1  and l.active = 1 and  (l.url is not null or l.hls is not null or l.hlsHd is not null) " +
            "order by l.aid,l.publicShow, l.createTime desc", nativeQuery = true)
    List<LiveRoom> findByAreaIdAndChildrenId(String[] childrenId);

    @Query(value = "select l.* from liveroom l " +
//            "left join area a on l.aid = a.id " +
            "left join `teaching_area` ta on ta.`id` = l.`teachingAreaId` " +
            "where (teachingAreaId in (" +
            "select teachingAreaId from rel_classes_teaching_area " +
            "where classesId in (" +
            "select classesId from rel_classes_children where childrenId in ?1)) or teachingAreaId in (" +
            "select id from teaching_area where areaId in (select aid from children where id in ?1) and isPublic = 1" +
            ")) and l.status = 1 and l.active = 1 and (l.url is not null or l.hls is not null or l.hlsHd is not null) " +
            "order by  l.orderNum asc , l.aid, ta.`isPublic`, l.createTime desc", nativeQuery = true)
    List<LiveRoom> findDOsByAreaIdAndChildrenId(String[] childrenId);


    LiveRoom findByIdAndAreaId(String id, String areaId);

    List<LiveRoom> findAllByIpUrlAndIpAuthAndStatusAndActiveIsTrueAndUrlIsNotNull(String ipUrl, String ipAuth, int status);

}
